import Vuex from 'vuex'
import Vue from 'vue'
import axios from 'axios'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
      isNavHead:true,//是否有头部导航
      isLoging:false,//是否为登录状态
      isLoginBox:false,//登录弹框
      isRegisBox:false,//注册弹框
      userId:'',//用户id
      userName:'',//用户名
      articleNumber:'',
      headerImg:''//头像地址
    },
    getters:{

    },
    mutations:{
      //登录弹框
      LOGINBOX(state,value){
        state.isLoginBox = value;
      },
      //注册弹框
      ISREGISBOX(state,value){
        state.isRegisBox = value;
      },
      // 是否登录
      ISLOGING(state,value){
        state.isLoging = value;
      },
      // 用户id
      USERID(state,value){
        state.userId = value;
      },
      // 头部导航显示与隐藏
      ISNAVHEAD(state,value){
        state.isNavHead = value;
      },
      USERNAME(state,value){
        state.userName = value;
      },
      HEADERIMG(state,value){
        state.headerImg = value;
      }
    },
    actions:{
       // 检测是否是否登录
      isloging({commit}){
        var that = this
        axios.get('/api/islogin').then(function(response){
          if(response.data.state==='1'){
            commit('ISLOGING',true);
            commit('USERID',response.data.id);
            commit('USERNAME',response.data.username);
            commit('HEADERIMG',response.data.headerImg);
          }else{
            commit('ISLOGING',false);   
          }
        })
      },
      // 退出登录
    }
  
});

export default store
  